from machine import Pin, PWM
import time

leds = [
    PWM(Pin(16, Pin.OUT), freq=1000, duty_u16=0),
    PWM(Pin(17, Pin.OUT), freq=1000, duty_u16=0),
    PWM(Pin(18, Pin.OUT), freq=1000, duty_u16=0)
]
key1 = Pin(12, Pin.IN, Pin.PULL_UP)  # 按钮1引脚
key2 = Pin(14, Pin.IN, Pin.PULL_UP)  # 按钮2引脚

data = 0.1  # 初始值为0，即按钮1未按下

def button_callback(pin):
    global data
    if pin.value() == 0:  # 再次确认按钮状态
        if pin == key1:
            data = round(min(data + 0.01, 0.1), 2)
        elif pin == key2:
            data = round(max(data - 0.01, 0.01), 2)

if __name__ == "__main__":
    key1.irq(button_callback, Pin.IRQ_FALLING)
    key2.irq(button_callback, Pin.IRQ_FALLING)

    while True:
        print(f"data: {data}")  # 打印当前data的值
        for led in leds:
            led.duty_u16(0)  # 将亮度设置为0

        for i in leds:
            for j in range(0, 65535, 10000):
                i.duty_u16(j)
                time.sleep(0.01)  # 控制呼吸灯的速度
            time.sleep(data)  # 控制呼吸灯的速度
            i.duty_u16(0)  # 将亮度设置为0
            for j in range(65535, 0, -10000):
                i.duty_u16(j)
                time.sleep(0.01)
            time.sleep(data)  # 控制呼吸灯的速度
            i.duty_u16(0)  # 将亮度设置为0